package com.smartdio.company;

import java.util.Vector;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "com.smartdio.company";

	public static final String EDITOR_EXTENTION_ID = "com.smartdio.company.editors";

	// The shared instance
	private static Activator plugin;

	/**
	 * The constructor
	 */
	public Activator() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
	 * )
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
	 * )
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	/**
	 * 通过图像ID注册单个图像的方法
	 * 
	 * @param keyid
	 */
	private void putImage(String keyid) {
		ImageDescriptor img = Resources.createImageDescriptorFor(keyid);
		getImageRegistry().put(keyid, img);
	}

	/**
	 * 根据图像ID获取Image对象
	 * 
	 * @param keyid
	 * @return
	 */
	public Image getImage(String keyid) {
		// 尝试获取图像
		Image img = getImageRegistry().get(keyid);
		if (img == null) { // 返回null，说明还没有注册
			putImage(keyid); // 注册图像
			return getImageRegistry().get(keyid);
		} else {
			return img;
		}
	}



	/**
	 * 获取所有personEditor扩展的设置
	 * @return
	 */
	public IConfigurationElement[] getPersonEditors() {
		Vector<IConfigurationElement> modifiers = new Vector<IConfigurationElement>();

		// 从系统的ExtensionRegistry中获取指定扩展点的所有设置子元素
		IConfigurationElement[] els = Platform.getExtensionRegistry()
				.getConfigurationElementsFor(EDITOR_EXTENTION_ID);
		// 找出名称为personEditor的子元素
		for (IConfigurationElement ele : els) {
			if (ele.getName().equals("personEditor")) {
				modifiers.add(ele);
			}
		}
		
		// 转换为数组返回
		IConfigurationElement[] result = new IConfigurationElement[modifiers
				.size()];
		for (int i = 0; i < result.length; i++) {
			result[i] = modifiers.get(i);
		}

		return result;
	}
	
	/**
	 * 获取所有personModifier扩展的设置
	 * @return
	 */
	public IConfigurationElement[] getPersonModifiers() {

		Vector<IConfigurationElement> modifiers = new Vector<IConfigurationElement>();

		IConfigurationElement[] els = Platform.getExtensionRegistry()
				.getConfigurationElementsFor(EDITOR_EXTENTION_ID);
		for (IConfigurationElement ele : els) {
			if (ele.getName().equals("personModifier")) {
				modifiers.add(ele);
			}
		}

		IConfigurationElement[] result = new IConfigurationElement[modifiers
				.size()];
		for (int i = 0; i < result.length; i++) {
			result[i] = modifiers.get(i);
		}

		return result;
	}
}
